package com.ad.admin.interview;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

public class QuestionActivity extends BaseActivity {
    ListView list ;
    SimpleAdapter adapter;
    List<Map<String,Object>> values;

    private int pageSize = 15;
    private int pageNum = 1;
    private int recordCount;
    private int pageCount;
    private String keyword;

    private int first;
    private int visible;
    private int total;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        init("面试试题", 1);

        keyword = getIntent().getStringExtra("keyword");
        if(keyword==null) {
            keyword = "";
        }
        values = QuestionDao.loadDataPage(pageNum, pageSize, keyword);
        recordCount = QuestionDao.getCount(keyword);
        pageCount = (recordCount - 1) / pageSize + 1;

        list = (ListView)findViewById(R.id.question_list);
        final TextView footView = new TextView(this);
        footView.setText("正在查询，请稍后.....");
        footView.setTextSize(12);
        footView.setTextColor(Color.BLACK);
        list.addFooterView(footView);

        adapter = new SimpleAdapter(this,values,R.layout.question_list,new String[]{"question"},new int[]{R.id.question_tv});
        list.setAdapter(adapter);
        Log.d("Values.size............", values.size() + "");
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = values.get(position);
                LinearLayout layout = (LinearLayout) view;

                if (layout.getChildCount() == 1) {
                    Log.d("answer.....", map.get("answer") + "");
                    TextView textView = new TextView(QuestionActivity.this);
                    textView.setText(map.get("answer").toString());
                    textView.setTextSize(14);
                    textView.setTextColor(Color.RED);
                    layout.addView(textView);
                } else {
                    layout.removeViewAt(1);
                }
            }
        });
        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState==SCROLL_STATE_IDLE&&first+visible==total&&total!=0){
                    if(pageNum<pageCount){
                        pageNum++;
                        values.addAll(QuestionDao.loadDataPage(pageNum,pageSize,keyword));
                        adapter.notifyDataSetChanged();
                    }else {
                        if(list.getFooterViewsCount()>0){
                            list.removeFooterView(footView);
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                first = firstVisibleItem;
                visible = visibleItemCount;
                total = totalItemCount;
            }
        });
    }
}
